package operation;

import book.Book;
import book.Booklist;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lenovo
 * Date: 2022-07-04
 * Time: 9:15
 */
public class DelOperation implements IOperation{
    @Override
    public void work(Booklist booklist) {
        System.out.println("删除图书");
        System.out.println("请输入你要删除的书名");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int current = booklist.getUesdsize();
        int index=-1;
        int i = 0;
        for ( ;i < current; i++) {
            Book book = booklist.getBook(i);
            if(book.getName().equals(name)){
                index = i;
                break;
            }
        }

        //1.i>=current 没找到这本书
        if(i>= current){
            System.out.println("没有找到这本书");
            return;
        }
        //2.break 找到了这本书 叫这本书的下标存放在index里
        i = index;
        for (int j = index; j < current-1; j++) {
            Book book = booklist.getBook(j+1);
            booklist.setBook(i,book);

        }

        booklist.setUesdsiz(current-1);//书的数量-1
        booklist.setBook(current-1,null);//删除的书籍进行了移动，最后一个位置置为空
        System.out.println("删除成功");
    }
}
